package com.wn.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wn.bean.Building;
import com.wn.bean.R;
import com.wn.mapper.BuildingMapper;
import com.wn.service.BuildService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author yellow Docter
 * @date 2022 -07-12
 * @desc
 */
@Service
public class BuildServiceImpl implements BuildService {

    @Autowired
    private BuildingMapper buildingMapper;

    @Override
    public R findBuild(Integer currentPage, Integer pageSize) {

        R r = new R();

        //分页操作
        //分页
        Page<Building> page = PageHelper.startPage(currentPage, pageSize).doSelectPage(() -> buildingMapper.findAll());
//        List<Building> list = buildingMapper.findAll();

        //分页的 展示的数据
        List<Building> data = page.getResult();
        // 分页查询的总数
        long total = page.getTotal();

        r.setMsg("查询成功");
        r.setCode(100000);

        HashMap<String, Object> map = new HashMap<>();
        map.put("data",data);
        map.put("total",total);
        r.setData(map);
        return r;
    }

    @Override
    public R buildDeleteById(Integer id) {
        int i = buildingMapper.buildDeleteById(id);

        return i > 0 ? R.OK() :R.ERROR();
    }

    /**
     * buildFindById 通过id查询
     * @param id
     * @return
     */
    @Override
    public R buildFindById(Integer id) {
        Building building = buildingMapper.buildFindById(id);
        return R.OK(building);
    }

    /**
     * 修改操作
     * @param build
     * @return
     */
    @Override
    public R updateBuild(Building build) {
        int row = buildingMapper.updateBuild(build);
        return row > 0 ? R.OK():R.ERROR();
    }

    @Override
    public R addBuilding(Building build) {
        int row = buildingMapper.addBuilding(build);
        return row > 0 ? R.OK():R.ERROR();
    }

    @Override
    public R deleteRows(List<Integer> ids) {
        int row = buildingMapper.deleteRows(ids);
        return  row > 0 ? R.OK():R.ERROR();
    }
}
